package de.tum.in.eist.poll.client.views;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.in.eist.poll.client.controllers.Controller;
import de.tum.in.eist.poll.client.views.widgets.SpinBoxWidget;
import de.tum.in.eist.poll.client.views.widgets.StudentListWidget;
import de.tum.in.eist.poll.shared.beans.Student;

/**
 * The UserManagement Page
 * @author Sebastian Hofstetter
 *
 */
public class UserManagementView extends View {
	private VerticalPanel userManagementPanel = new VerticalPanel();
	
	private StudentListWidget studentListWidget;
	private SpinBoxWidget amount = new SpinBoxWidget(0,1000,1);
	private Button addButton = new Button("Add Students");
	private Button removeButton = new Button("Remove All");
	
	public UserManagementView() {
		// load assigned students of the lecturer
		Controller.userManagmentController.getStudents(new AsyncCallback<List<Student>>() {

			@Override
			public void onFailure(Throwable caught) {}

			@Override
			public void onSuccess(List<Student> result) {
				final Student[] students = new Student[result.size()];
				result.toArray(students);
				studentListWidget = new StudentListWidget(students);	// the table of the students
				
				// addButton
				addButton.addClickHandler(new ClickHandler() {
					@Override
					public void onClick(ClickEvent event) {
						// add students
						Controller.userManagmentController.addStudents(amount.getValue(), new AsyncCallback<List<Student>>() {

							@Override
							public void onFailure(Throwable caught) {}

							@Override
							public void onSuccess(List<Student> result) {
								Controller.controller.refreshPage();
							}
						});
					}
				});
				
				// removeButton
				removeButton.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						String rStudents[] = new String[students.length];
						for(int i = 0; i<students.length; i++) {
							rStudents[i] = students[i].getUserID();
						}
						Controller.userManagmentController.removeUsers(rStudents, new AsyncCallback<String>(){

							@Override
							public void onFailure(Throwable caught) {}

							@Override
							public void onSuccess(String result) {
								Controller.controller.refreshPage();
							}
							
						});
					}
				});
				
				userManagementPanel.add(studentListWidget);
				
				
				HorizontalPanel addStudentsBox = new HorizontalPanel();
				addStudentsBox.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
				addStudentsBox.add(new Label("Amount: "));
				addStudentsBox.add(amount);
				addStudentsBox.add(addButton);
				addStudentsBox.add(removeButton);
				
				userManagementPanel.add(addStudentsBox);
				
			}
		});
		
		
		initWidget(userManagementPanel);
		userManagementPanel.addStyleName("userManagementView");
	}
}
